home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / r3sim.arc / SIM_MEM.C < prev    next >
C/C++ Source or Header  |  1989-07-08  |  6KB  |  260 lines

  1. #include <stdio.h>
  2. #include "sim.h"
  3.  
  4. #ifdef MSC
  5. #include <graph.h>
  6. #endif
  7.  
  8. unsigned char mem[ 4095];
  9.  
  10. int sim_read( addr)
  11. int addr;
  12. {
  13.    int data, done;
  14.    char c;
  15.    char buff[80];
  16.  
  17.     done = data = 0;
  18.     /* Don't prompt user for values if we are disassembling */
  19.     if (!disassemble)
  20.     {
  21.     if (addr >= 0 && addr < 4)
  22.     {
  23.         if (mem[addr+4] != 0xFF)   /* FF in DDR = all outputs */
  24.         {
  25.         c = (char) ((int)'A' + addr);
  26.         sprintf(buff, "Enter read value for port %c: ", c);
  27.         showprompt( buff);
  28.         fgets( buff, 80, stdin);
  29.         sscanf( buff, "%d", &data);
  30.         }
  31.         data = data & (~mem[addr+4]) | mem[addr] & mem[addr+4];
  32.         clearline( PROMPTLINE);
  33.         done = 1;
  34.     }
  35.     else if (addr == AD_REG)
  36.     {
  37.         showprompt( "Enter read value for ADC register: ");
  38.         fgets( buff, 80, stdin);
  39.         sscanf( buff, "%d", &data);
  40.         clearline( PROMPTLINE);
  41.         done = 1;
  42.     }
  43.     }
  44.     if (done) goto readend;
  45.     if (addr >= 4 && addr < 8)
  46.     data = 0xFF;    /* port data direction regs are write only */
  47.     else if ((addr == TIMER_CONTROL) && (mem[MASK_OPT_REG] & 0x40))
  48.     data = (mem[addr] & 0xF7) | 0x37;
  49.     else if (addr >= 0 && addr <= 4095)
  50.     data = mem[addr];
  51.     else if (addr == RW_A)
  52.     data = pgm_model.a;
  53.     else if (addr == RW_X)
  54.     data = pgm_model.x;
  55.     else if (addr == RW_SP)
  56.     data = pgm_model.sp;
  57. readend:
  58.     return(data);
  59. }
  60.  
  61. int sim_write( addr, data)
  62. int addr, data;
  63. {
  64.     if (!disassemble)
  65.     {
  66.     if (addr >= 0 && addr <= 0x7F)  /* permit write to RAM, ports only */
  67.         mem[addr] = data;
  68.     else if (addr == RW_A)
  69.         pgm_model.a = data;
  70.     else if (addr == RW_X)
  71.         pgm_model.x = data;
  72.     else if (addr == RW_SP)
  73.         pgm_model.sp = data;
  74.     if (addr == TIMER_CONTROL)
  75.         sim_timer_ctrl();
  76.     }
  77. }
  78.  
  79. int sim_writef( addr, data)
  80. int addr, data;
  81. /* write forced - Just like sim_write but can write to PROM */
  82. /* Called from non-simulation things like 'M' (mem mod) and 'L' (load sx) */
  83. {
  84.     if (addr <= 0x7F)
  85.     sim_write( addr, data);   /* handle special cases, all in RAM, I/O */
  86.     else
  87.     {
  88.         mem[addr] = data;     /* Always succeeds - can write to PROM */
  89.     }
  90. }
  91.  
  92. int sim_readf( addr)
  93. int addr;
  94. /* read forced - simply returns mem[addr] - no prompts, no nothing */
  95. /* Used by sim_timer to read TCR as if it were the timer - the user */
  96. /* program cannot read most bits of the TCR in MOR controlled mode */
  97. {
  98.     return( mem[addr] );
  99. }
  100.  
  101. int sim_bitread( addr, bit)
  102. int addr, bit;
  103. /* If bit is an output bit in a port, no need to ask user for value */
  104. /* in that case don't call sim_read, or it would ask */
  105. /* NOTE: returns byte value just like sim_read, not just one bit */
  106. /*    The bit argument is only used to check for out bit in port */
  107. /*    This is so the trace can show the value of the byte, which */
  108. /*    the processor really reads */
  109. {
  110.     int data;
  111.  
  112.     if (( addr >= 0 && addr < 4) && (mem[addr+4] & (1<<bit)) )
  113.     data = mem[addr];
  114.     else
  115.         data = sim_read( addr);
  116.     return( data);
  117. }
  118.  
  119. /*********************************************************************/
  120. int load_moto_hex( infile)
  121. FILE *infile;
  122. /* opens infile and reads S0, S1 and S9 records.  S0 records are ignored.
  123.    S9 records terminate input.  Checksums on all records are ignored.
  124. */
  125. {
  126.     int i, j, addr, data, state, count, error, cc, s9;
  127.  
  128.     state = 0;
  129.     s9 = 0;
  130.  
  131.     while ( (i = fgetc( infile)) != EOF && !s9)
  132.     {
  133.     j = (i >= 'A') ? i - 55 : i - '0'; /* if i is hex digit, j is value*/
  134.     switch( state)
  135.     {
  136.     case 0:       /* Look for 'S' = beginning of record */
  137.         if (i == 'S')
  138.         {
  139. #ifdef DEBUG
  140.     printf( "Found 'S'-");
  141. #endif
  142.         state = 1;
  143.         count = 0;
  144.         cc = 0;
  145.         addr = 0;
  146.         }
  147.         break;
  148.     case 1:       /* Get character after 'S' = record type */
  149.         if (i == '1')
  150.         state = 2;
  151.         else if (i == '9')
  152.         s9 = 1;       /* exit input loop */
  153.         else
  154.         state = 0;  /* Skip S0 records - look for next 'S' */
  155.         break;
  156.     case 2:      /* Get byte count for this record */
  157. #ifdef DEBUG
  158.     printf( "Count-");
  159. #endif
  160.         count <<= 4;
  161.         count += j;
  162.         cc++;
  163.         if (cc == 2)
  164.         {
  165. #ifdef DEBUG
  166.     printf( "  Count = %d\n", count);
  167. #endif
  168.         state = 3;
  169.         addr = 0;
  170.         cc = 0;
  171.         }
  172.         break;
  173.     case 3:     /* Get start address for this record */
  174. #ifdef DEBUG
  175.     printf( "Addr-");
  176. #endif
  177.         addr <<= 4;
  178.         addr += j;
  179.         cc++;
  180.         if (cc == 4)
  181.         {
  182.         state = 4;
  183.         data = 0;
  184.         cc = 0;
  185. #ifdef DEBUG
  186.     printf( "  Addr = %d\n", addr);
  187. #endif
  188.         count -= 2;
  189.         if (count == 1)
  190.             state = 0;     /* Skip checksum, look for 'S' */
  191.         }
  192.         break;
  193.     case 4:     /* Get data bytes, 2 chars each */
  194. #ifdef DEBUG
  195.     printf( "Data-");
  196. #endif
  197.         data <<= 4;
  198.         data += j;
  199.         cc++;
  200.         if (cc == 2)
  201.         {
  202.         sim_writef( addr, data);
  203. #ifdef DEBUG
  204.     printf( "Wrote %d to %d\n", data, addr);
  205. #endif
  206.         addr++;
  207.         data = 0;
  208.         cc = 0;
  209.         count--;
  210.         if (count == 1)
  211.             state = 0;  /* Skip checksum, look for 'S' */
  212.         }
  213.         break;
  214.     }  /* end switch */
  215.     } /* end while */
  216.     return( !s9);       /* Error is didn't end with S9 record */
  217. } /* end load_moto_hex */
  218.  
  219. int load( filename)
  220. char filename[];
  221. /* returns 0 if no error, 1 if error */
  222. {
  223.     FILE *infile;
  224.     int retval;
  225.     char fname[64];
  226.     char *cP;
  227.     int i; 
  228.  
  229.     i = 0;
  230.     cP = fname;
  231.     while (filename[i] != '\n' && i < 64)
  232.     {
  233.     if (filename[i] != ' ' && filename[i] != '\x09')  /* skip white */
  234.         *cP++ = filename[i];
  235.     i++;
  236.     }
  237.     *cP++ = '\0';   /* Null terminate string */
  238.     
  239.     infile = fopen( fname, "r");
  240.     if (infile != NULL)
  241.     retval = load_moto_hex( infile);
  242.     else
  243.     retval = 2;
  244.     settextposition( PROMPTLINE,1);
  245.     if (retval)
  246.     {
  247.        if (retval == 2)
  248.        printf  ("*** CANNOT OPEN FILE %s ***", fname);
  249.        else
  250.        printf( "*** ERROR LOADING FILE %s ***", fname);
  251.     }
  252.     else
  253.     {
  254.        printf( "File %s loaded", fname);
  255.        sim_reset();
  256.     }
  257.     promptline_full = 1;
  258.     return( retval);
  259. }
  260.